<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Installation {
    private $CI;
    private $appkey=null;
    private $host;
    private $user;
    private $password;
    private $dbname;

    const CONFIG_FILENAME = "sm-receiver-config.ini";
    const CONFIG_FILEPATH = "/SMReceiver/";
    const SM_VERSION = "1.0.0";

    function  __construct() {
        $this->CI =& get_instance();
    }

    function doInstall($_POST){
        $this->host = $_POST['txtHost'];
        $this->user = $_POST['txtUser'];
        $this->password = $_POST['txtPassword'];
        $this->dbname = $_POST['txtDatabaseName'];

        // set application key
        $this->appkey = md5($this->user . time());

        // connect to database
        $this->connect($this->host, $this->user, $this->password, $this->dbname);

        // clear tables
        $this->dropConfigTable();
        $this->dropMessageTypesTable();

        // create tables
        $this->createConfigTable();
        $this->createDomainsTable();
        $this->createMessagesTable();
        $this->createMessageTypesTable();

        // create sm-receiver-config.ini file
        $this->clearConfigFile();
        $this->createConfigFile();
        return true;
    }

    function clearConfigFile(){
        if(file_exists(DOCUMENT_ROOT . self::CONFIG_FILEPATH . self::CONFIG_FILENAME)){
            // remove file
            unlink(DOCUMENT_ROOT . self::CONFIG_FILEPATH . self::CONFIG_FILENAME);
        }
    }

    function createConfigFile(){
        $handle = fopen(DOCUMENT_ROOT . self::CONFIG_FILEPATH . self::CONFIG_FILENAME, "w");
        $config_contents = "
; this is a config file for the sm receiver app

[database_configuration]
db_host = \"" . $this->host . "\"
db_user = \"" . $this->user . "\"
db_pw = \"" . $this->password . "\"
db_name = \"" . $this->dbname . "\"";

        if(!fwrite($handle, $config_contents)){
            die("could not create " . self::CONFIG_FILENAME . " file");
        }
        fclose($handle);
    }

    function dropConfigTable(){
        $action = "DROP TABLE IF EXISTS config;";
        if(!$this->CI->db->simple_query($action)){
            die("unable to drop config table<br><br>" . mysql_error());
        }
    }

    function dropMessageTypesTable(){
        $action = "DROP TABLE IF EXISTS message_types;";
        if(!$this->CI->db->simple_query($action)){
            die("unable to drop message_types table<br><br>" . mysql_error());
        }
    }

    function createConfigTable(){
        $action = "CREATE TABLE IF NOT EXISTS `config` (
                  `appkey` varchar(250) NOT NULL,
                  `sm_version` varchar(10) NOT NULL
                ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
        if(!$this->CI->db->simple_query($action)){
            die("unable to create config table<br><br>" . mysql_error());
        }else{
            $action = "INSERT INTO config (appkey, sm_version) VALUES
                    ('". $this->appkey . "', '" . self::SM_VERSION . "');";
            error_log("action: " . $action);
            $this->CI->db->simple_query($action);
        }
    }

    function createDomainsTable(){
        $action = "CREATE TABLE IF NOT EXISTS `domains` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `domain_name` varchar(100) NOT NULL,
                  `date_created` datetime NOT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        if(!$this->CI->db->simple_query($action)){
            die("unable to create domains table<br><br>" . mysql_error());
        }
    }

    function createMessagesTable(){
        $action = "CREATE TABLE IF NOT EXISTS `messages` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `domain` varchar(100) NOT NULL,
                  `message` mediumtext NOT NULL,
                  `message_type` int(11) NOT NULL,
                  `message_file` varchar(250) NOT NULL,
                  `message_line` int(11) NOT NULL,
                  `php_self` varchar(250) NOT NULL,
                  `date_created` datetime NOT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
        if(!$this->CI->db->simple_query($action)){
            die("unable to create messages table<br><br>" . mysql_error());
        }
    }

    function createMessageTypesTable(){
        $action = "CREATE TABLE IF NOT EXISTS `message_types` (
                      `id` int(11) NOT NULL,
                      `type` varchar(100) NOT NULL
                    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
        if(!$this->CI->db->simple_query($action)){
            die("unable to create message_types table<br><br>" . mysql_error());
        }else{
            $action = "INSERT INTO `message_types` (`id`, `type`) VALUES
                        (1, 'PHP Error'),
                        (2, 'PHP Warning'),
                        (3, 'PHP Notice'),
                        (4, 'PHP Unknown'),
                        (5, 'PHP Strict');";
            $this->CI->db->simple_query($action);
        }
    }

    function connect($host, $user, $pass, $dbname){
        $config['hostname'] = $host;
        $config['username'] = $user;
        $config['password'] = $pass;
        $config['database'] = $dbname;
        $config['dbdriver'] = "mysql";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";

        $this->CI->load->database($config);
    }
}

?>